package br.ufmg.ubicomp.droidguide.application;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;

/**
 * Uses the MapPoint Web Service to get details (of nearby locations/addresses) on a specified lat/long
 */
public class ReverseGeoCoder extends Activity {
//implements View.OnClickListener {

    // UI elements
    private Button mLookup;
    private EditText mUserId;
    private EditText mPassword;
    private EditText mLatitude;
    private EditText mLongitude;

//    public void onCreate(Bundle icicle) {
//        super.onCreate(icicle);
//        setContentView(R.layout.main);
//
//        // Gather the troops
//        mUserId = (EditText) findViewById(R.id.username);
//        mPassword = (EditText) findViewById(R.id.password);
//        mLatitude = (EditText) findViewById(R.id.latitude);
//        mLongitude = (EditText) findViewById(R.id.longitude);
//        mLookup = (Button) findViewById(R.id.lookup);
//
//        // Set up a couple of button listeners
//        mLookup.setOnClickListener(this);
//    }
//
//    // SOAP Request for the FindServiceSOAP.GetLocationInfo web service
//    String soapRequestXML_1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//            "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
//            "               xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
//            "               xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
//            "  <soap:Body>\n" +
//            "    <GetLocationInfo xmlns=\"http://s.mappoint.net/mappoint-30/\">\n" +
//            "      <location>";
//    String soapRequestXML_2 = "      </location>\n" +
//            "      <dataSourceName>MapPoint.NA</dataSourceName>\n" +
//            "      <options/>\n" +
//            "    </GetLocationInfo>\n" +
//            "  </soap:Body>\n" +
//            "</soap:Envelope>";
//
//    /**
//     * When the user clicks on the lookup button we use the MapPoint SOAP API to fetch the
//     * information of the specified latitude longitude.
//     *
//     * @param view
//     */
//    public void onClick(View view) {
//
//        String user = mUserId.getText().toString();
//        String password = mPassword.getText().toString();
//        String latitude = mLatitude.getText().toString();
//        String longitude = mLongitude.getText().toString();
//
//        // Use our customized Digest Auth scheme as per the following article
//        // http://go.mappoint.net/MappointMPAC/default.aspx?main=article.aspx&id=J10002
//        AuthPolicy.registerAuthScheme(AuthPolicy.DIGEST, DigestMapPointScheme.class);
//
//        String url = "http://findv3.staging.mappoint.net/Find-30/FindService.asmx";
//        HttpClient client = new HttpClient();
//        client.setConnectionTimeout(8000);
//
//        Credentials credentials = new UsernamePasswordCredentials(user, password);
//        client.getState().setCredentials(AuthScope.ANY, credentials);
//
//        PostMethod postMethod = new PostMethod(url);
//
//        // Construct a SOAP request by hand
//        StringBuffer request = new StringBuffer();
//        request.append(soapRequestXML_1);
//        request.append("<Latitude>").append(latitude).append("</Latitude>");
//        request.append("<Longitude>").append(longitude).append("</Longitude>");
//        request.append(soapRequestXML_2);
//        postMethod.setRequestBody(request.toString());
//        postMethod.setRequestHeader("Content-Type",
//                "text/xml; charset=utf-8");
//        postMethod.setRequestHeader("SOAPAction",
//                "\"http://s.mappoint.net/mappoint-30/GetLocationInfo\"");
//
//        int statusCode = 0;
//        try {
//            statusCode = client.executeMethod(postMethod);
//        } catch (IOException e) {
//            Log.d("ReverseGeoCoder", e.toString(), e);
//        }
//
//        Log.i("ReverseGeoCoder", "statusCode>>>" + statusCode);
//        Log.i("ReverseGeoCoder", "statusLine>>>" + postMethod.getStatusLine());
//
//        // Parse the SOAP Response
//        MyContentHandler myContentHandler = new MyContentHandler();
//        try {
//            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
//            parser.parse(postMethod.getResponseBodyAsStream(), myContentHandler);
//        } catch (Exception e) {
//            Log.d("ReverseGeoCoder", e.toString(), e);
//        }
//
//        // Display the response details.
//        List list = myContentHandler.getLocations();
//        String[] items = new String[list.size()];
//        for (int i = 0; i < list.size(); i++) {
//            MyContentHandler.MyLocation location = (MyContentHandler.MyLocation) list.get(i);
//            Log.i("ReverseGeoCoder", "Location : " + i);
//            Log.i("ReverseGeoCoder", "Latitude : " + location.latitude);
//            Log.i("ReverseGeoCoder", "Longitude : " + location.longitude);
//            Log.i("ReverseGeoCoder", "Description : " + location.description);
//            items[i] = location.description + " (geo:" + location.latitude + "," + location.longitude + ")";
//        }
//        // Show the data in the list view
//        ListView listView = (ListView) findViewById(R.id.data);
//        listView.setAdapter(new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1,
//                items));
//        postMethod.releaseConnection();
//    }
}